/**
 * @file MutexLock.cc
 * @author zziywang (zziywang@163.com)
 * @brief { life is too short to learn cpp! }
 * @version 1.0
 * @date 2022-05-08
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "MutexLock.hh"
#include <stdio.h>

namespace snow
{

MutexLock::MutexLock()
{
    int ret = pthread_mutex_init(&mutex_, nullptr);
    if (0 != ret) {
        perror("pthread_mutex_init");
    }
}
MutexLock::~MutexLock()
{
    int ret = pthread_mutex_destroy(&mutex_);
    if (0 != ret) {
        perror("pthread_mutex_destroy");
    }
}

void MutexLock::lock()
{
    int ret = pthread_mutex_lock(&mutex_);
    if (0 != ret) {
        perror("pthread_mutex_lock");
        return;
    }
}
void MutexLock::unlock()
{
    int ret = pthread_mutex_unlock(&mutex_);
    if (0 != ret) {
        perror("pthread_mutex_unlock");
        return;
    }
}

pthread_mutex_t *MutexLock::getMutexPtr()
{
    return &mutex_;
}

MutexLockGuard::MutexLockGuard(MutexLock &mutex)
    : mutex_(mutex)
{
    mutex_.lock();
}
MutexLockGuard::~MutexLockGuard()
{
    mutex_.unlock();
}

} // namespace snow